<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>产品生产工序</title>
</head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css"
      rel="stylesheet">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-table.min.css">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="../js/bootstrap-table.min.js"></script>
<script src="../js/bootstrap-table-zh-CN.min.js"></script>


<body>

<div class="modal fade m-auto" id="myModal">
    <div class="modal-dialog modal-xl" style="width: 1000px;height: 800px">
        <div class="modal-content">

            <!--     制作设计单模态框       -->
            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">生产工序设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="m-auto row">
            <!--  获取DFile id  -->
                    <span id="parentId" style="display: none"></span>
                    <span id="firstKindId" style="display: none"></span>
                    <span id="firstKindName" style="display: none"></span>
                    <span id="secondKindId" style="display: none"></span>
                    <span id="secondKindName" style="display: none"></span>
                    <span id="thirdKindId" style="display: none"></span>
                    <span id="thirdKindName" style="display: none"></span>


                    <div class="col"><span>产品名称:</span><span id="name"></span></div>
                    <div class="col"><span>产品编号:</span><span id="id"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <span class="col-2">设计人:</span><input id="sheji">
                </div>
                <div style="padding-top: 15px">
                    <table id="tab1" class="table table-striped table-bordered table-hover">
                        <thead style="background-color: #aeaeae;border: 1px solid black">
                        <tr>
                            <th>点选</th>
                            <th>工序名称</th>
                            <th>工序编号</th>
                            <th>描述</th>
                            <th>工时数</th>
                            <th>工时单位</th>
                            <th>单位工时成本</th>
                            <th>工时成本小计(元)</th>
                        </tr>
                        </thead>
                        <tbody id="product">
                        <tr>
                            <td><input class="checkd" id="assemble" type="checkbox" style="width: 30px;"></td>
                            <td class="procedureName">组装</td>
                            <td class="procedureId">001</td>
                            <td class="procedureDescribe"><input type="text" style="width: 90px;"></td>
                            <td class="labourHourAmount"><input class="calculate" id="zgss" type="number"
                                                                style="width: 90px;"></td>
                            <td class="amountUnit"><input type="text" style="width: 90px;"></td>
                            <td class="costPrice"><input class="calculate" id="zcb" type="number" style="width: 90px;">
                            </td>
                            <td class="subtotal"><span id="zSum">0</span></td>
                        </tr>
                        <tr>
                            <td><input class="checkd" id="test" type="checkbox" style="width: 30px;"></td>
                            <td class="procedureName">测试</td>
                            <td class="procedureId">002</td>
                            <td class="procedureDescribe"><input type="text" style="width: 90px;"></td>
                            <td class="labourHourAmount"><input class="calculate" id="cgss" type="number"
                                                                style="width: 90px;"></td>
                            <td class="amountUnit"><input type="text" style="width: 90px;"></td>
                            <td class="costPrice"><input class="calculate" id="ccb" type="number" style="width: 90px;">
                            </td>
                            <td class="subtotal"><span id="cSum">0</span></td>
                        </tr>
                        <tr>
                            <td><input class="checkd" id="packaging" type="checkbox" style="width: 30px;"></td>
                            <td class="procedureName">包装</td>
                            <td class="procedureId">003</td>
                            <td class="procedureDescribe"><input type="text" style="width: 90px;"></td>
                            <td class="labourHourAmount"><input class="calculate" id="bgss" type="number"
                                                                style="width: 90px;"></td>
                            <td class="amountUnit"><input type="text" style="width: 90px;"></td>
                            <td class="costPrice"><input class="calculate" id="bcb" type="number" style="width: 90px;">
                            </td>
                            <td class="subtotal"><span id="bSum">0</span></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div>
                        <span class="col">登记人:</span><span id="person"></span>
                        <span class="col">登记时间:</span><span id="time"></span>
                    </div>
                </div>
                <div class="row m-auto" style="padding-top: 15px">
                    <span class="col-3">设计要求:</span><textarea id="procedureDescribe"
                                                              style="width: 500px; height: 100px;"></textarea>
                </div>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="DFileObj.combination()">提交</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>


        </div>
    </div>
</div>

<nav class="breadcrumb">
    <a class="breadcrumb-item" href="#">生产管理</a>
    <a class="breadcrumb-item" href="#">产品工序设计</a>
    <a class="breadcrumb-item" active href="#">制定产品生产工序设计单</a>
</nav>

<div class="input-group mb-3" style="width: 50%">
    <div class="input-group-prepend">
        <span class="input-group-text">产品编号</span>
    </div>
    <input id="productId" type="text" class="form-control">
    &nbsp;
    <div class="input-group-prepend">
        <span class="input-group-text">产品名称</span>
    </div>
    <input id="productName" type="text" class="form-control">
    &nbsp;
    <button onclick="$('#tab').bootstrapTable('refresh')" class="btn btn-primary">查询</button>
</div>

<div>
    <table id="tab">

    </table>
</div>
</body>

<script>
    //失去焦点计算成本
    $(".calculate").on('blur', function () {
        DFileObj.Sum();
    })

    var DFileObj = {
        DFileText: function (dFile) {
            //制作设计单传值
            $("#name").text(dFile.productName)//产品名称
            $("#id").text(dFile.productId)//产品编号
            $("#time").text(new Date().toLocaleString())//传入登记时间
            $("#person").text(dFile.register)//登记人
            $("#parentId").text(dFile.id)//id

            $("#firstKindId").text(dFile.firstKindId)//产品i级分类编号
            $("#firstKindName").text(dFile.firstKindName)//产品i级分类名称
            $("#secondKindId").text(dFile.secondKindId)//产品ii级分类编号
            $("#secondKindName").text(dFile.secondKindName)//产品ii级分类名称
            $("#thirdKindId").text(dFile.thirdKindId)//产品iii级分类编号
            $("#thirdKindName").text(dFile.thirdKindName)//产品iii级分类名称

            //把值赋上去在打开模态框 js
            $('#myModal').modal("show");



        },
        //计算成本
        Sum: function () {
            if (($("#zgss").val() > 0) && ($("#zcb").val() > 0)) {
                $("#zSum").text(parseInt($("#zgss").val()) * parseInt($("#zcb").val()));
            }
            if (($("#cgss").val() > 0) && ($("#ccb").val() > 0)) {
                $("#cSum").text(parseInt($("#cgss").val()) * parseInt($("#ccb").val()));
            }
            if (($("#bgss").val() > 0) && ($("#bcb").val() > 0)) {
                $("#bSum").text(parseInt($("#bgss").val()) * parseInt($("#bcb").val()));
            }
        },
        //计算工时总成本
        combination: function () {

            if ($("input[type='checkbox']:checked").length > 0) {
                var count = 0;
                $.each($("input[type='checkbox']:checked"), function (i, n) {
                    var $obj = $(n);
                    count += parseInt($obj.parent('td').siblings().find('span').text());
                })



                var mDesignProcedure = {
                    productId: $("#id").text(),//产品编号
                    productName: $("#name").text(),//产品名称
                    designer: $("#sheji").val(),//设计人
                    register: $("#person").text(),//登记人
                    registerTime: $("#time").text(),//登记时间
                    procedureDescribe: $("#procedureDescribe").val(),//设计要求
                    firstKindId:$("#firstKindId").text(),//产品i级分类编号
                    firstKindName:$("#firstKindName").text(),//产品i级分类名称
                    secondKindId:$("#secondKindId").text(),//产品ii级分类编号
                    secondKindName:$("#secondKindName").text(),//产品ii级分类名称
                    thirdKindId:$("#thirdKindId").text(),//产品iii级分类编号
                    thirdKindName:$("#thirdKindName").text(),//产品iii级分类名称
                    costPriceSum: count//工时总成本

                }
                //添加到工序表
                $.ajax({
                    type: "post",
                    contentType: "application/json;charset=utf-8",
                    url: "/AddMDesignProcedure.action",
                    data: JSON.stringify(mDesignProcedure),
                    success: function (id) {
                        //获取tr里面标签的值
                        let tableHtml = $(document).find('#product>tr');
                        let mDesignProcedureDetails = getTableDate(tableHtml, count,id);
                        console.log(JSON.stringify(mDesignProcedureDetails))
                        //添加到工序明细表
                        $.ajax({
                            type: "post",
                            url: "/AddMDesignProcedureDetails.action",
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(mDesignProcedureDetails),
                            success: function (i) {
                                    //修改DFILE表 工序组成标志 已设计
                                    var id = $("#parentId").text()
                                    $.ajax({
                                        type: "post",
                                        contentType: "application/json;charset=utf-8",
                                        url: "/UpdateDFileTag.action",
                                        data: JSON.stringify(id),
                                        success:function (){
                                            $('#tab').bootstrapTable('refresh')
                                        }
                                    })

                            }
                        })
                    }
                })
                /**
                 * 获取表格数据
                 * @param tableHtml
                 */
                function getTableDate(tableHtml, count,id) {
                    let result = [];
                    if (tableHtml) {
                        let length = tableHtml.length;
                        for (let i = 0; i < length; i++) {//追加数据
                            let trData = {};//每行的数据
                            let checd = $(tableHtml).eq(i).find('.checkd');
                            if (checd.prop("checked") == true) {
                                let procedureName = $(tableHtml).eq(i).find('.procedureName').text();//工序名称
                                let procedureId = $(tableHtml).eq(i).find('.procedureId').text();//工序编号
                                let procedureDescribe = $(tableHtml).eq(i).find('.procedureDescribe>input').val();//工序描述
                                let labourHourAmount = $(tableHtml).eq(i).find('.labourHourAmount>input').val();//工时数
                                let amountUnit = $(tableHtml).eq(i).find('.amountUnit>input').val()//单位
                                let costPrice = $(tableHtml).eq(i).find('.costPrice>input').val();//工时成本
                                let subtotal = $(tableHtml).eq(i).find('.subtotal>span').text();//工时成本小计
                                let register = $("#person").text()//登记人
                                let registerTime = $("#time").text();//登记时间

                                trData.procedureName = procedureName;
                                trData.procedureId = procedureId;
                                trData.procedureDescribe = procedureDescribe;
                                trData.labourHourAmount = labourHourAmount;
                                trData.amountUnit = amountUnit;
                                trData.costPrice = costPrice;
                                trData.subtotal = subtotal;
                                trData.parentId = id;
                                trData.register = register;
                                trData.registerTime = registerTime;
                                trData.moduleSubtotal = count;
                                result.push(trData)
                            }

                        }
                    }
                    return result;
                }

                $('#myModal').modal("hide");
            } else {
                alert("至少选中一个工序才能提交")
            }

        }
    }

</script>

<script>
    $(function () {

        $("#tab").bootstrapTable({
            method: "post",
            url: "/queryDFileAll.action",
            striped: true,
            pageNumber: 1,
            pagination: true,
            sidePagination: "server",
            pageSize: 3,
            pageList: [3, 6, 9],
            queryParams: function (i) {
                return {
                    //计算当前页
                    // currentPage:(parts.offset/parts.limit)+1,
                    pageNumber: i.offset,
                    //每条页数
                    pageSize: i.limit,
                    productId: $("#productId").val(),
                    productName: $("#productName").val()
                }
            },
            columns: [
                {title: "产品编号", field: "productId"},
                {title: "产品名称", field: "productName"},
                {
                    title: "档次级别", field: "productClass", formatter: function (value, row, index) {
                        if (value == "D001-1") {
                            return "高档";
                        } else if (value == "D001-2") {
                            return "中档";
                        } else {
                            return "低档";
                        }
                    }
                },
                {title: "I级分类", field: "firstKindName"},
                {title: "II级分类", field: "secondKindName"},
                {title: "II级分类", field: "thirdKindName"},
                {title: "产品经理", field: "responsiblePerson"},
                {
                    title: "产品设计单", field: "id", align: "center",
                    formatter: function (value, row, index) {
                        return "<button onclick='DFileObj.DFileText(" + JSON.stringify(row) + ")' class=\"btn btn-primary btn-sm\" data-toggle=\"modal\">制定设计单</button>";
                    }
                }
            ]
        })
    })

</script>
</html>